package com.zl.tcbnewcar.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zl.tcbnewcar.R;
import com.zl.tcbnewcar.base.BaseActivity;
import com.zl.tcbnewcar.model.CityBean;
import com.zl.tcbnewcar.ui.dialog.ChooseAddressDialog;
import com.zl.tcbnewcar.ui.fragment.ServiceListFragment;
import com.zl.tcbnewcar.utils.SpUtlis;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 * @date 18/3/20
 */
public class ServiceListActivity extends BaseActivity {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_city)
    TextView tvCity;
    private ServiceListFragment serviceListFragment;
    private int type;
    private ChooseAddressDialog addressDialog;
    private CityBean mCityBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_list);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {
        addressDialog.setOnAddressDataListener(new ChooseAddressDialog.OnAddressDataListener() {
            @Override
            public void addressData(CityBean cityBean) {
                mCityBean = cityBean;
                tvCity.setText(cityBean.getCityName());
                search();
            }
        });
    }

    private void initView() {
        type = getIntent().getIntExtra("type", -1);
        CityBean cuLocationData = SpUtlis.getCuLocationData(mActivity);
        etSearch.setHint("您想要搜索的服务");
        tvCity.setText(cuLocationData.getCityName());
        addressDialog = new ChooseAddressDialog(mActivity);
        serviceListFragment = ServiceListFragment.newInstance(type, cuLocationData.getCityCode());
        openFragmentNoTask(serviceListFragment, R.id.service_list_rl);
    }

    @OnClick({R.id.iv_back, R.id.tv_search, R.id.tv_city})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
                search();
                break;
            case R.id.tv_city:
                addressDialog.showDialog(view);
                break;
        }
    }

    /**
     * 检索数据
     */
    private void search() {
        String searchData = etSearch.getText().toString().trim();
        serviceListFragment.updateData(searchData, mCityBean != null ? mCityBean.getCityCode() : "");
    }

}
